package com.google.ma.rssreader;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

public class NewsSelectActivity extends Activity {

    private static final int CONTEXTMENU_DELETEITEM = 1;

    private static final int CONTEXTMENU_CANCEL = 2;

    private ListView newsSelectListView;

    private ArrayList<HashMap<String, String>> dataList;

    private RssDAO rssDAO;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_select);

        newsSelectListView = (ListView) findViewById(R.id.NewsSelectListView);

        rssDAO = new RssDAO(NewsSelectActivity.this);
        renderNewsSelectList();

        newsSelectListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String url = dataList.get(position).get("url");
                Uri uri = Uri.parse(url);
                Intent intent = new Intent();
                intent.setData(uri);
                intent.setClass(NewsSelectActivity.this, NewsListActivity.class);
                startActivity(intent);
            }

        });

        newsSelectListView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
                menu.setHeaderTitle(R.string.operation);
                menu.add(0, CONTEXTMENU_DELETEITEM, 0, R.string.delete);
                menu.add(0, CONTEXTMENU_CANCEL, 0, R.string.cancel);
            }

        });
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
        case CONTEXTMENU_DELETEITEM:
            String title = ((HashMap<String, String>) newsSelectListView.getAdapter().getItem(menuInfo.position)).get("title");
            rssDAO.delete(title);
            renderNewsSelectList();
            return true;
        case CONTEXTMENU_CANCEL:
            return true;
        }
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, 1, 0, R.string.add_rss);
        menu.add(0, 2, 1, R.string.menu_exit);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
        case 1:
            Intent intent = new Intent();
            intent.setClass(NewsSelectActivity.this, AddRssActivity.class);
            startActivity(intent);
            break;
        case 2:
            System.exit(0);
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
        case RESULT_OK:
            renderNewsSelectList();
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onResume() {
        renderNewsSelectList();
        super.onResume();
    }

    private void renderNewsSelectList() {
        dataList = rssDAO.queryList(null);

        SimpleAdapter adapter = new SimpleAdapter(NewsSelectActivity.this, dataList, R.layout.news_select_item, new String[] { "title" }, new int[] { R.id.NewsSelectTitleTextView });

        newsSelectListView.setAdapter(adapter);
    }
}
